package com.dl698.bean;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;

/**
 * 帧数据类
 * 用于封装解析结果和状态码
 * 
 * @author wind
 * @version 1.1.1
 */
public class FrameData {

    private Object result;
    private int resultCode;

    // 定义dataKey枚举
    public enum dataKey {
        null_data,
        array,
        structure,
        bool,
        bit_string,
        double_long,
        double_long_unsigned,
        octet_string,
        visible_string,
        utf8_string,
        integer,
        long_key("long"),
        unsigned,
        long_unsigned,
        long64,
        long64_unsigned,
        enum_key("enum"),
        float32,
        float64,
        date_time,
        date,
        time,
        date_time_s,
        oi,
        oad,
        road,
        omd,
        ti,
        tsa,
        mac,
        rn,
        region,
        scaler_unit,
        rsd,
        csd,
        ms,
        sid,
        sid_mac,
        comdcb,
        rcsd;

        private final String value;

        dataKey() {
            this.value = this.name();
        }

        dataKey(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return value;
        }
    }

    /**
     * 默认构造函数
     */
    public FrameData() {
    }

    /**
     * 构造函数
     * 
     * @param result     结果数据
     * @param resultCode 结果码
     */
    public FrameData(Object result, int resultCode) {
        this.result = result;
        this.resultCode = resultCode;
    }

    /**
     * 获取数据
     * 
     * @return 结果数据
     */
    public Object getData() {
        return result;
    }

    /**
     * 设置数据
     * 
     * @param result 结果数据
     */
    public void setData(Object result) {
        this.result = result;
    }

    /**
     * 获取结果码
     * 
     * @return 结果码
     */
    public int getCode() {
        return resultCode;
    }

    /**
     * 设置结果码
     * 
     * @param resultCode 结果码
     */
    public void setCode(int resultCode) {
        this.resultCode = resultCode;
    }

    /**
     * 设置结果码
     * 
     * @param resultCode 结果码枚举
     */
    public void setCode(com.dl698.tools.ResultCode resultCode) {
        this.resultCode = resultCode.getValue();
    }

    /**
     * 转换为JSON字符串
     * 
     * @return JSON字符串
     */
    public String toJsonString() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", this.resultCode);
        map.put("data", this.result);
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(map);
        } catch (Exception e) {
            return "{\"code\": " + this.resultCode + ", \"data\": " + this.result + "}";
        }
    }

    @Override
    public String toString() {
        return "FrameData{" +
                "result=" + result +
                ", resultCode=" + resultCode +
                '}';
    }
}